/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package database.tables;

import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import database.DBManager;
import database.objects.DOMapResult;
import database.objects.DOUserMap;
/**
 *
 * @author Tri
 */
public class DTUserMap {
    private DB m_db;
    
    public DTUserMap() {
        m_db = DBManager.getInstance().getDB();
    }
        
    public DOUserMap getUserMap(String mapID) {
        DOUserMap map = null;
        BasicDBObject searchQuery = new BasicDBObject().append(DOUserMap.KEY_MAP_ID, mapID);
        DBCursor cursor = getCollection().find(searchQuery);
        if(cursor.count() > 0) {
            BasicDBObject result = (BasicDBObject)cursor.curr();
            String ownerID = result.getString(DOUserMap.KEY_OWNER_ID);
            int betMoney = result.getInt(DOUserMap.KEY_BET_MONEY);
            int completeTime = result.getInt(DOUserMap.KEY_COMPLETE_TIME);
            int completeSteps = result.getInt(DOUserMap.KEY_COMPLETE_STEPS);
            int oneStar = result.getInt(DOUserMap.KEY_ONE_STAR);
            int twoStar = result.getInt(DOUserMap.KEY_TWO_STAR);
            int threeStar = result.getInt(DOUserMap.KEY_THREE_STAR);
            int fourStar = result.getInt(DOUserMap.KEY_FOUR_STAR);
            int fiveStar = result.getInt(DOUserMap.KEY_FIVE_STAR);
            String matrix = result.getString(DOUserMap.KEY_MAP_ID);
            BasicDBList listObstacle = (BasicDBList)result.get(DOUserMap.KEY_OBSTACLES_LIST);
            String[] strListObstacles = new String[listObstacle.size()];
            for (String s : strListObstacles) {
               BasicDBObject subObject = new BasicDBObject();
               
            }
            /*
             public String mapName;
    private String ownerID;
    public  int betMoney;
    public int completeStep;
    public int completeTime;
    public String[] obstaclesList;
    public int oneStar;
    public int twoStar;
    public int threeStar;
    public int fourStar;
    public int fiveStar;
    public String matrix;
             */
        }
        return map;
    }
    
    public void insertMap(DOUserMap map) {
        BasicDBObject dBObject = new BasicDBObject();
        dBObject.put(DOUserMap.KEY_MAP_ID, map.getMapID());
        dBObject.put(DOUserMap.KEY_OWNER_ID, map.getOwnerID());
        dBObject.put(DOUserMap.KEY_BET_MONEY, map.betMoney);
        dBObject.put(DOUserMap.KEY_COMPLETE_STEPS, map.completeStep);
        dBObject.put(DOUserMap.KEY_COMPLETE_TIME, map.completeTime);
        dBObject.put(DOUserMap.KEY_MATRIX, map.matrix);
        for (String s : map.obstaclesList) {
            BasicDBObject subObject = new BasicDBObject();
            subObject.put(DOUserMap.KEY_SUB_OBSTACLE, s);
            dBObject.put(DOUserMap.KEY_OBSTACLES_LIST, subObject);
        }
        dBObject.put(DOUserMap.KEY_ONE_STAR, map.oneStar);
        dBObject.put(DOUserMap.KEY_TWO_STAR, map.twoStar);
        dBObject.put(DOUserMap.KEY_THREE_STAR, map.threeStar);
        dBObject.put(DOUserMap.KEY_FOUR_STAR, map.fourStar);
        dBObject.put(DOUserMap.KEY_FIVE_STAR, map.fiveStar);
        getCollection().insert(dBObject);
    }
    
    public boolean deleteMap(String mapID) {
        BasicDBObject dbObject = new BasicDBObject().append(DOUserMap.KEY_MAP_ID, mapID);
        DBCursor cursor = getCollection().find(dbObject);
        if(cursor.count() > 0) {
            getCollection().remove(cursor.curr());
            return true;
        }
        
        return false;
    }
    
    public void updateMap(DOUserMap map) {
        BasicDBObject searchQuery = new BasicDBObject().append(DOUserMap.KEY_MAP_ID, map.getMapID());
        DBCursor cursor = getCollection().find(searchQuery);
        if(cursor.count() > 0) {
            BasicDBObject updateQuery = new BasicDBObject();
            updateQuery.put(DOUserMap.KEY_BET_MONEY, map.betMoney);
            updateQuery.put(DOUserMap.KEY_COMPLETE_STEPS, map.completeStep);
            updateQuery.put(DOUserMap.KEY_COMPLETE_TIME, map.completeTime);
            updateQuery.put(DOUserMap.KEY_MATRIX, map.matrix);
            updateQuery.put(DOUserMap.KEY_OBSTACLES_LIST, map.obstaclesList);
            updateQuery.put(DOUserMap.KEY_ONE_STAR, map.oneStar);
            updateQuery.put(DOUserMap.KEY_TWO_STAR, map.twoStar);
            updateQuery.put(DOUserMap.KEY_THREE_STAR, map.threeStar);
            updateQuery.put(DOUserMap.KEY_FOUR_STAR, map.fourStar);
            updateQuery.put(DOUserMap.KEY_FIVE_STAR, map.fiveStar);
            for (String s : map.obstaclesList) {
                BasicDBObject subObject = new BasicDBObject();
                subObject.put(DOUserMap.KEY_SUB_OBSTACLE, subObject);
                updateQuery.put(DOUserMap.KEY_OBSTACLES_LIST, subObject);
            }
            getCollection().update(searchQuery, updateQuery);
        }
    }

    public DBCollection getCollection() {
        return m_db.getCollection("UserMap");
    }
}
